<template>
	<view>
		<view class="input-wrap" @click="handleChooseImg">
			<view class="l-wrap">头像</view>
			<view class="r-wrap">
				<view class="avatar">
					<image :src="avatar" mode=""></image>
				</view>
				<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
			</view>
		</view>

		<view class="input-wrap" @click="handleGoUserNickname">
			<view class="l-wrap">昵称</view>
			<view class="r-wrap">
				<text class="txt">{{nickName}}</text>
				<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
			</view>
		</view>

		<view class="input-wrap">
			<view class="l-wrap">实名认证</view>
			<view v-if="AuthorizeStatus==1" class="r-wrap" @click="handleGoRealNameAuthor">
				<text class="txt status">未认证</text>
				<uni-icons type="arrowright" size="20" color="#999"></uni-icons>
			</view>
			<view v-else class="r-wrap">
				<text class="txt status">已认证</text>
				<!-- <uni-icons type="arrowright" size="20" color="#999"></uni-icons> -->
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				avatar: '',
				nickName: '',
				AuthorizeStatus: 1,
			};
		},
		onLoad(opt) {
			if(opt.type && opt.type == 2038){
				uni.showToast({
					title: '请修改头像',
					icon: 'none'
				})
			}
		},
		onShow() {
			this.getUserInfo()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getUserInfo();
		},
		methods: {
			handleChooseImg() {
				let _this = this
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], //从相册选择
					success: ({tempFilePaths}) => {
						uni.uploadFile({
							url: _this.$baseUrl + _this.$api.uploadFile,
							filePath: tempFilePaths[0],
							name: 'file',
							// formData: {
							// 	'user': 'test'
							// },
							success: (uploadFileRes) => {
								let uploadData = JSON.parse(uploadFileRes.data)
								console.log('uploadData',uploadData)
								_this.$http.get(_this.$api.EditAvatar, {avater: uploadData.url}).then(res => {
									if (res.code === 200) {
										_this.getUserInfo()
									}
								})
							}
						})
					}
				});
			},
			handleGoUserNickname() {
				uni.navigateTo({
					url: './userNickname'
				});
			},
			handleGoRealNameAuthor() {
				uni.navigateTo({
					url: './realNameAuthentication',
				})
			},
			getUserInfo() {
				this.$http.get(this.$api.getUserInfo).then(res => {
					uni.stopPullDownRefresh();
					if (res.code === 200) {
						if(!res.data.avatar.startsWith('http')){
							this.avatar = this.$imgUrl + res.data.avatar
						}
						this.nickName = res.data.nickname
						this.AuthorizeStatus = res.data.assetsStatus
						this.$forceUpdate()
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.input-wrap {
		width: 690rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		font-size: 30rpx;
		margin: 20rpx auto;
		border-radius: 12rpx;
		background-color: #fff;
		height: 88rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.l-wrap {}

		.r-wrap {
			display: flex;
			flex: 1;
			align-items: center;
			justify-content: flex-end;

			.avatar {
				height: 48rpx;
				width: 48rpx;
				border-radius: 50%;
				overflow: hidden;
				margin-right: 10rpx;
			}

			.txt {
				margin-right: 10rpx;

				&.status {
					color: $yellow;
					font-size: 26rpx;
				}
			}
		}
	}
</style>
